using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Data.SQLite;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_VerifyLoop : Form
    {
        public Sec_Scan_VerifyLoop()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        public int oldX, oldY;
        public int secrs = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            //验证POC的方式
            if(string.IsNullOrEmpty(watermarkTextBox4.Text) || string.IsNullOrEmpty(watermarkTextBox6.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                if(comboBox1.Text == "GET")
                {
                    PocHistoryInfoGet();
                }
                else
                {
                    if (string.IsNullOrEmpty(richTextBox1.Text))
                    {
                        MessageBox.Show("POST数据不能为空！");
                    }
                    else
                    {
                        //是POST请求的情况下 执行POST请求
                        PocHistoryInfoPost();
                    }

                }
            }
        }
        /// <summary>
        /// POC的历史信息 需要存放到数据库
        /// </summary>
        public void PocHistoryInfoGet()
        {
            richTextBox2.Clear();//执行前进行一个清空操作
            string url = watermarkTextBox4.Text;//url地址
            string api = watermarkTextBox6.Text;//请求接口
            int swgf = Convert.ToInt32(watermarkTextBox2.Text);//获取int类型格式
            secrs = swgf * 1000;//把获取到的结果拿来乘以 1000 也就是把秒 转 毫秒
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + api);
                if (string.IsNullOrEmpty(watermarkTextBox8.Text))
                {
                    request.Method = "GET";
                    request.Timeout = secrs;//设置超时
                    request.UserAgent = watermarkTextBox7.Text;
                    request.Referer = watermarkTextBox9.Text;//设置refer
                    request.Headers.Add("cookie:" + watermarkTextBox5.Text);//添加cookie
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream receiveStream = response.GetResponseStream();
                    StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("UTF-8"));
                    string html = readStream.ReadToEnd();
                    string sload = html;//获取源代码
                    richTextBox2.Text = sload.ToString();
                    //执行成功后 将数据存入到数据库中 
                    PocTestInfoAddSql();
                }
                else
                {
                    request.Method = "GET";
                    request.Timeout = secrs;//设置超时
                    request.UserAgent = watermarkTextBox7.Text;
                    request.Host = watermarkTextBox8.Text;//设置host地址
                    request.Referer = watermarkTextBox9.Text;//设置refer
                    request.Headers.Add("cookie:" + watermarkTextBox5.Text);//添加cookie
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream receiveStream = response.GetResponseStream();
                    StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("UTF-8"));
                    string html = readStream.ReadToEnd();
                    string sload = html;//获取源代码
                    richTextBox2.Text = sload.ToString();
                    //执行成功后 将数据存入到数据库中 
                    PocTestInfoAddSql();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// POST请求
        /// </summary>
        public void PocHistoryInfoPost()
        {
            richTextBox2.Clear();//执行前进行一个清空操作
            string url = watermarkTextBox4.Text;//url地址
            string api = watermarkTextBox6.Text;//请求接口
            string urls = url + api;
            int swgf = Convert.ToInt32(watermarkTextBox2.Text);//获取int类型格式
            secrs = swgf * 1000;//把获取到的结果拿来乘以 1000 也就是把秒 转 毫秒
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urls);
                if (string.IsNullOrEmpty(watermarkTextBox8.Text))
                {
                    request.Method = "POST";
                    request.Timeout = secrs;//设置超时
                    request.UserAgent = watermarkTextBox7.Text;
                    request.Referer = watermarkTextBox9.Text;//设置refer
                    request.Headers.Add("cookie:" + watermarkTextBox5.Text);//添加cookie
                    Encoding encoding = Encoding.GetEncoding("UTF-8");
                    byte[] data = encoding.GetBytes(request + richTextBox1.Text);//richtextbox1 指POST参数
                    request.ContentLength = data.Length;
                    Stream outstream = null;
                    Stream instream = null;
                    StreamReader sr = null;
                    HttpWebResponse response = null;
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Flush();
                    outstream.Close();
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    richTextBox2.Text = content.ToString();
                    PocTestInfoAddSql();
                }
                else
                {
                    request.Method = "POST";
                    request.Timeout = secrs;//设置超时
                    request.UserAgent = watermarkTextBox7.Text;
                    request.Host = watermarkTextBox8.Text;//设置host地址
                    request.Referer = watermarkTextBox9.Text;//设置refer
                    request.Headers.Add("cookie:" + watermarkTextBox5.Text);//添加cookie
                    Encoding encoding = Encoding.GetEncoding("UTF-8");
                    byte[] data = encoding.GetBytes(request + richTextBox1.Text);//richtextbox1 指POST参数
                    request.ContentLength = data.Length;
                    Stream outstream = null;
                    Stream instream = null;
                    StreamReader sr = null;
                    HttpWebResponse response = null;
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Flush();
                    outstream.Close();
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    richTextBox2.Text = content.ToString();
                    PocTestInfoAddSql();
                }    
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 将测试数据添加到数据库
        /// </summary>
        public void PocTestInfoAddSql()
        {
            string path = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopNumberApi.M1);//连接漏洞数据库
            string base1 = Sec_Config.Sec_Base64Code.Tobase64(richTextBox2.Text);
            SQLiteConnection connection = new SQLiteConnection(path);
            try
            {
                connection.Open();
                if(connection.State == ConnectionState.Open)
                {
                    //如果能打开的情况下执行下面的事件
                    string sql = "INSERT INTO Sec_Scan_VerifyLoop (testaddress, cookie, testapi, testuseragent, testpost, testhost, testrefer, testname, restimeout, resqtimeout, restype, testbody, addtime) VALUES('"+watermarkTextBox4.Text+"', '"+watermarkTextBox5.Text+"', '"+watermarkTextBox6.Text+"', '"+watermarkTextBox7.Text+"', '"+richTextBox1.Text+"', '"+watermarkTextBox8.Text+"', '"+watermarkTextBox9.Text+"', '"+watermarkTextBox1.Text+"', '"+watermarkTextBox2.Text+ "', '" + watermarkTextBox3.Text + "', '"+comboBox1.Text+"', '"+base1+"', '"+DateTime.Now+"')";//获取相关测试数据 并进行添加到数据库中
                    SQLiteCommand command = new SQLiteCommand(sql,connection);
                    command.ExecuteReader();
                    Console.WriteLine("数据已保存到数据库");
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //判断是否为鼠标左键
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }
    }
}
